package com.ccloud.modules.system.controller;

import com.ccloud.api.system.mapper.SysDictMapper;
import com.ccloud.api.system.model.vo.DuplicateCheckVo;
import com.ccloud.common.core.reqres.response.Result;
import com.ccloud.common.core.utils.SqlInjectionUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * The type Duplicate check controller.
 */
@Slf4j
@RestController
@RequestMapping("/duplicate")
@Api(tags = "重复校验")
public class DuplicateCheckController {

    /**
     * The Sys dict mapper.
     */
    @Autowired
    SysDictMapper sysDictMapper;

    /**
     * 校验数据是否在系统中是否存在
     *
     * @param duplicateCheckVo the duplicate check vo
     * @return the result
     */
    @RequestMapping(value = "/check", method = RequestMethod.GET)
    @ApiOperation("重复校验接口")
    public Result<Object> doDuplicateCheck(DuplicateCheckVo duplicateCheckVo) {
        int num = 0;
        log.info("----duplicate check------：" + duplicateCheckVo.toString());
        SqlInjectionUtil.filterContent(new String[]{duplicateCheckVo.getTableName(), duplicateCheckVo.getFieldName()});
        if (StringUtils.isNotBlank(duplicateCheckVo.getDataId())) {
            // [2].编辑页面校验
            num = sysDictMapper.duplicateCheckCountSql(duplicateCheckVo);
        } else {
            // [1].添加页面校验
            num = sysDictMapper.duplicateCheckCountSqlNoDataId(duplicateCheckVo);
        }
        if (num == 0) {
            // 该值可用
            return Result.ok();
        } else {
            // 该值不可用
            return Result.error(HttpServletResponse.SC_OK, "该值不可用，系统中已存在！");
        }
    }
}
